﻿using PIMS.DBModel;
using PIMS.ORM;
using PIMS.Utils;
using PIMS.XmlModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PIMS
{
    /// <summary>
    /// 登陆窗口，可登陆用户，可登出用户。
    /// </summary>
    public partial class FrmLogin : Form
    {
        public UserInfo UserLogin = new UserInfo();

        private bool isAutoLoadAllUser = false;

        public FrmLogin()
        {
            InitializeComponent();

            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            if (Common.pimsConfig.GetAppValueByKey("IsAutoLoadAllUser") == "1")
            {
                this.isAutoLoadAllUser = true;
                this.tbUser.Visible = false;
                this.txtUserName.Visible = true;
            }
            else
            {
                this.tbUser.Visible = true;
                this.txtUserName.Visible = false;
            }
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            if (Common.pimsConfig.GetAppValueByKey("english") == "1")
            {
                this.Text = "LOGIN"; //用户登陆
                this.label1.Text = "UserName："; //用户名：
                this.label2.Text = "Password："; //密  码：
                this.btnLogin.Text = "Login"; //登陆
                this.btnLogout.Text = "Logout"; //登出
                this.btnExit.Text = "Close"; //离开
            }

            try
            {
                if (isAutoLoadAllUser)
                {
                    List<users> allUser = SqlSugarHelper.QueryList<users>();
                    foreach (users u in allUser)
                    {
                        txtUserName.Items.Add(u.UserName + " " + "班组:" + u.Department);
                    }
                    tbUser.Text = allUser.First().UserName;
                    txtUserName.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = "";
            if (isAutoLoadAllUser)
            {
                userName = this.txtUserName.Text;
            }
            else
            {
                userName = this.tbUser.Text;
            }
            if (userName == string.Empty)
            {
                MessageBox.Show("用户名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            if (userName.Contains(" "))
            {
                int pos = userName.IndexOf(" ");
                userName = userName.Substring(0, pos);
            }

            users currentUser = SqlSugarHelper.Query<users>(string.Format("UserName='{0}'", userName), "");

            if (currentUser == null)
            {
                MessageBox.Show(string.Format("用户名{0}不存在！", userName), "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            if (txtPassword.Text.ToUpper() != currentUser.Password.ToUpper())
            {
                MessageBox.Show("密码错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            UserLogin.UserName = currentUser.UserName;
            UserLogin.Password = "";
            UserLogin.Description = currentUser.Description;
            UserLogin.Department = currentUser.Department;
            UserLogin.Level = currentUser.Level;

            this.DialogResult = DialogResult.OK;
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("确定登出当前用户么？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                UserLogin.clear();
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.btnLogin_Click(null, null);
            }
        }
    }
}
